package com.blb.dao.admin.impl;

import com.blb.dao.admin.CarouselDao;
import com.blb.pojo.Carousel;
import com.blb.utils.JDBCUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class CarouselDaoImpl implements CarouselDao {

    @Override
    public Carousel selectBySeq(int seq) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        Carousel carousel = JDBCUtils.getOne(connection, Carousel.class, "select * from t_carousel where seq = ?",seq);
        connection.close();
        return carousel;
    }



    @Override
    public List<Carousel> selectPage(int page, int size) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from t_carousel order by seq asc limit ?,?";
        List<Carousel> list = JDBCUtils.getList(connection, Carousel.class, sql,(page-1)*size,size);
        connection.close();
        return list;
    }

    @Override
    public int count() throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(id) count from t_carousel ";

        ResultSet resultSet = JDBCUtils.prepareStatement(connection, sql).executeQuery();
        resultSet.next();
        int count = resultSet.getInt("count");
        connection.close();
        return count;
    }

    @Override
    public void insert(Carousel carousel) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "insert into t_carousel(id  ,image_url  ,jump_url ,seq) values(?,?,?,?)",
                carousel.getId(), carousel.getImageUrl(), carousel.getJumpUrl(),carousel.getSeq()).executeUpdate();
        connection.close();
    }

    @Override
    public void deleteById(int id) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "delete from t_carousel where id = ?", id).executeUpdate();
        connection.close();
    }

    @Override
    public void batchDelete(String[] ids) throws SQLException, ClassNotFoundException {
        String sql = "delete from t_carousel where id in (";

        for (int i = 0; i < ids.length; i++) {
            sql+="?";
            if (i != ids.length-1){
                sql+=",";
            }
        }
        sql+=")";

        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection,sql,ids).executeUpdate();
        connection.close();
    }
    
    @Override
    public Carousel selectById(int id) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        Carousel carousel = JDBCUtils.getOne(connection, Carousel.class, "select * from t_carousel where id = ?", id);
        connection.close();
        return carousel;
    }
    
    @Override
    public void update(Carousel carousel) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection,"update t_carousel set image_url=?,jump_url=?,seq=? where id=?"
                ,carousel.getImageUrl(),carousel.getJumpUrl(),carousel.getSeq(),carousel.getId()).executeUpdate();
        connection.close();
    }

    @Override
    public List<Carousel> getCarousel() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from t_carousel order by seq asc ";
        List<Carousel> list = JDBCUtils.getList(connection, Carousel.class, sql);
        connection.close();
        return list;
    }
}
